Function.RegisterNamespace("System.IO.DirectoryStrategy");

System.IO.DirectoryStrategy.Xhr=function(){
    // IDirectoryStrategy Members
    this.Exists=function(path){
        path=System.IO.Path.GetFullPath(path);
        var transport=getTransport();
        transport.open('HEAD',String.Format("{0}?type=directory&path={1}",getTarget(),encodeURIComponent(path)),false);
        transport.send();
        if(!transport.status||transport.status==200)return true;
        return false;
    };

    this.GetFiles=function(path){
        return getList("files",path);
    };
    
    this.GetDirectories=function(path){
        return getList("directories",path);
    };

    // IStrategySpecification Members
    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        return getTransport()!=null;
    };

    // Private Methods
    function getList(type,path){
        path=System.IO.Path.GetFullPath(path);
        var transport=getTransport();
        if(transport.overrideMimeType)transport.overrideMimeType("text/plain");
        transport.open('GET',String.Format("{0}?type={1}&path={2}",getTarget(),encodeURIComponent(type),encodeURIComponent(path)),false);
        transport.send();
        if(!transport.status||transport.status==200){
            var list=String.Trim(transport.responseText).split('\n');
            for(var i=list.length-1;i>=0;i--){
                if(String.IsEmpty(list[i]))list.splice(i,1);
            }
            return list;
        }
        return null;
    }

    function getTarget(){
        if(!System.IO.DirectoryStrategy.Xhr.ResourceUri)throw new Error("'System.IO.DirectoryStrategy.Xhr.ResourceUri' must be set before invoking IDirectoryStrategy methods. Choose a resource type (.aspx, .jsp, .php) from XhrResource.");
        return System.IO.DirectoryStrategy.Xhr.ResourceUri;
    }

    function getTransport(){
        if(typeof(XMLHttpRequest)!='undefined')return new XMLHttpRequest();
        if(typeof(ActiveXObject)!='undefined')return new ActiveXObject('Microsoft.XMLHTTP');
        return null;
    }
};

System.IO.DirectoryStrategy.Xhr.Implement(System.IO.DirectoryStrategy.IDirectoryStrategy,'System.IO.DirectoryStrategy.Xhr');
System.IO.DirectoryStrategy.Xhr.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.DirectoryStrategy.Xhr');

System.IO.Directory.Strategies.Add(System.IO.DirectoryStrategy.Xhr);